import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import User from '../components/users/User.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/Login' },
  { path: '/Login', component: Login },
  {
    path: '/Home',
    component: Home,
    redirect: '/Welcome',
    children: [
      { path: '/Welcome', component: Welcome },
      { path: '/Users', component: User },
      { path: '/Rights', component: Rights },
      { path: '/Roles', component: Roles }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 路由导航守卫做路由限制让还没有登录的用户跳转到登录页面
router.beforeEach((to, from, next) => {
// to去某个页面
  // 如果用户去login则直接放行
  if (to.path === '/login') return next()
  // 从sessionStorage中取到token
  const tokenStr = window.sessionStorage.getItem('token')
  // 如果没有token则说明该用户没有登录就强制跳转到login中去
  if (!tokenStr) return next('/login')
  // 如果都不是则直接放行
  next()
})
export default router
